package com.hjqjl.module_home.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.hjqjl.module_home.databinding.HomeActivityMyscrollviewBinding;
import com.hjqjl.whapplib.base.BaseActivity;

public class MyScrollViewActivity extends BaseActivity<HomeActivityMyscrollviewBinding> {
    public static void start(Context context) {
        Intent starter = new Intent(context, MyScrollViewActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void initListener() {
        pVBinding.homeSvMyScrollview.setOnScrollListener((myscrollView, scrollY) -> {

            int mTitleHeight = pVBinding.homeLlMyScrollviewTitle.getHeight();
            int mSmallTitleHeight = pVBinding.homeLlMyScrollviewSmallTitle.getHeight();
            float cha = mTitleHeight - mSmallTitleHeight;//为正数

            //自动跳转到指定位置
            if (myscrollView.isScrollStop()) {
                myscrollView.setScrollStop(false);
                if (myscrollView.getStopY() < (cha / 2)) {//手离开触摸屏的时候，当悬浮小按钮位于大按钮中线上部的时候，ScrollView滚动到大按钮的顶部
                    myscrollView.smoothScrollTo(0, 0);
                    scrollY = 0;
                } else if (myscrollView.getStopY() < cha) {//手离开触摸屏的时候，当悬浮小按钮位于大按钮中线下部的时候，ScrollView滚动到大按钮的底部
                    myscrollView.smoothScrollTo(0, (int) cha);
                    scrollY = (int) cha;
                }
            }

            if (scrollY == 0) {
                pVBinding.homeLlMyScrollviewSmallTitle.setVisibility(View.GONE);
            } else if (scrollY > 0) {
                pVBinding.homeLlMyScrollviewSmallTitle.setVisibility(View.VISIBLE);
            }

            //设置透明度
            if (scrollY <= cha) {
                float mAlpha = scrollY / cha;
                if (mAlpha < 0) {
                    mAlpha = 0;
                } else if (mAlpha > 1) {
                    mAlpha = 1;
                }
                pVBinding.homeLlMyScrollviewSmallTitle.setAlpha(mAlpha);
            } else {
                pVBinding.homeLlMyScrollviewSmallTitle.setAlpha(1);
            }
        });
    }
}
